﻿using System;
using System.Text;
using System.Web;
using WebDotNet.Extensions;
using WebDotNet.Samples.DefaultMvcApplication.Models;
using WebDotNet.Samples.DefaultMvcApplication.ViewModels;

namespace WebDotNet.Samples.DefaultMvcApplication.Handlers.Account
{
	public class LogOn
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		public LogOn() : this(new FormsAuthenticationService(), new AccountMembershipService())
		{
		}

		public LogOn(IFormsAuthenticationService formsAuthenticationService, IMembershipService membershipService)
		{
			if (formsAuthenticationService == null) throw new ArgumentNullException("formsAuthenticationService");
			if (membershipService == null) throw new ArgumentNullException("membershipService");

			this.FormsService = formsAuthenticationService;
			this.MembershipService = membershipService;
		}

		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			return httpContextBase.RenderPage("Account/LogOn", new ViewModelBase() { HttpContextBase = httpContextBase });
		}

		public StringBuilder Post(HttpContextBase httpContextBase)
		{
			//if (ModelState.IsValid)
			//{
			//    if (MembershipService.ValidateUser(model.UserName, model.Password))
			//    {
			//        FormsService.SignIn(model.UserName, model.RememberMe);
			//        if (!String.IsNullOrEmpty(returnUrl))
			//        {
			//            return Redirect(returnUrl);
			//        }
			//        else
			//        {
			//            return RedirectToAction("Index", "Home");
			//        }
			//    }
			//    else
			//    {
			//        ModelState.AddModelError("", "The user name or password provided is incorrect.");
			//    }
			//}

			//// If we got this far, something failed, redisplay form
			//return View(model);

			return null;
		}
	}

	public class LogOff
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		public LogOff() : this(new FormsAuthenticationService(), new AccountMembershipService())
		{
		}

		public LogOff(IFormsAuthenticationService formsAuthenticationService, IMembershipService membershipService)
		{
			if (formsAuthenticationService == null) throw new ArgumentNullException("formsAuthenticationService");
			if (membershipService == null) throw new ArgumentNullException("membershipService");

			this.FormsService = formsAuthenticationService;
			this.MembershipService = membershipService;
		}

		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			this.FormsService.SignOut();
			httpContextBase.Response.Redirect("~/Home/Index");

			return null;
		}
	}

	public class Register
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		public Register() : this(new FormsAuthenticationService(), new AccountMembershipService())
		{
		}

		public Register(IFormsAuthenticationService formsAuthenticationService, IMembershipService membershipService)
		{
			if (formsAuthenticationService == null) throw new ArgumentNullException("formsAuthenticationService");
			if (membershipService == null) throw new ArgumentNullException("membershipService");

			this.FormsService = formsAuthenticationService;
			this.MembershipService = membershipService;
		}

		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			return httpContextBase.RenderPage("Account/Register", new RegisterViewModel() { PasswordLength = this.MembershipService.MinPasswordLength, HttpContextBase = httpContextBase });
		}

		public StringBuilder Post(HttpContextBase httpContextBase)
		{
			//if (ModelState.IsValid)
			//{
			//    // Attempt to register the user
			//    MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

			//    if (createStatus == MembershipCreateStatus.Success)
			//    {
			//        FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
			//        return RedirectToAction("Index", "Home");
			//    }
			//    else
			//    {
			//        ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
			//    }
			//}

			//// If we got this far, something failed, redisplay form
			//ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
			//return View(model);

			return null;
		}
	}

	public class ChangePassword
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		public ChangePassword()
			: this(new FormsAuthenticationService(), new AccountMembershipService())
		{
		}

		public ChangePassword(IFormsAuthenticationService formsAuthenticationService, IMembershipService membershipService)
		{
			if (formsAuthenticationService == null) throw new ArgumentNullException("formsAuthenticationService");
			if (membershipService == null) throw new ArgumentNullException("membershipService");

			this.FormsService = formsAuthenticationService;
			this.MembershipService = membershipService;
		}

		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			return httpContextBase.RenderPage("Account/ChangePassword", new { PasswordLength = this.MembershipService.MinPasswordLength });
		}

		public StringBuilder Post(HttpContextBase httpContextBase)
		{
			//if (ModelState.IsValid)
			//{
			//    if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
			//    {
			//        return RedirectToAction("ChangePasswordSuccess");
			//    }
			//    else
			//    {
			//        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
			//    }
			//}

			//// If we got this far, something failed, redisplay form
			//ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
			//return View(model);

			return null;
		}
	}

	public class ChangePasswordSuccess
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		public ChangePasswordSuccess()
			: this(new FormsAuthenticationService(), new AccountMembershipService())
		{
		}

		public ChangePasswordSuccess(IFormsAuthenticationService formsAuthenticationService, IMembershipService membershipService)
		{
			if (formsAuthenticationService == null) throw new ArgumentNullException("formsAuthenticationService");
			if (membershipService == null) throw new ArgumentNullException("membershipService");

			this.FormsService = formsAuthenticationService;
			this.MembershipService = membershipService;
		}

		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			return httpContextBase.RenderPage("Account/ChangePasswordSuccess", null);
		}
	}
}